with PES; use PES;
with Interfaces; use Interfaces;

package body Block_Filter is

   overriding procedure Found_Element(This : in out Block_Filter_Type; E : in Element_Type) is
      Cluster_Time : Element_Size_Type := 0;
      Block : Block_Type;
   begin

      case E.EBML_Type.ID is
         when 16#e7# => -- Cluster timecode
            Cluster_Time := E.Value(This.File);
            return;
         when 16#a0# => -- Block group
            return;
         when 16#a1# => -- Block
            Block_Header'Read(This.File.File_Access, Block.Header);
            Block.Size := E.Size;
            Block.File_Index := E.File_Index;
            This.Handle_Block(Block, Cluster_Time);
         when 16#fb# => -- Reference block
            Cluster_Time := E.Value(This.File);
            return;
         when others =>
            return;
      end case;

   end;

   procedure Handle_Block(This : in Block_Filter_Type; Block : in Block_Type; Cluster_Timecode : in Element_Size_Type) is
      Time_Code : Element_Size_Type;
      PES : PES_Type;
   begin
      -- TODO: remove hardcoding - implement for all wanted tracks
      if Block.Header.Track /= 2 then
         return;
      end if;
      Time_Code := Cluster_Timecode + Element_Size_Type(Block.Header.Timecode);


      PES := Create(Block     => Block,
                    Time_Code => Unsigned_32(Time_Code));

      -- Check for ref blocks for video
      -- Create PES or array of PES


   end Handle_Block;



end Block_Filter;
